﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace Bussiness
{
    /// <summary>
    /// 类TemplateFile。
    /// </summary>
    [Serializable]
    public partial class TemplateFile
    {
        public TemplateFile()
        { }
        #region Model
        private int _id;
        private string _filename;
        private string _tempname;
        private int _type;

        public int Type
        {
            get { return _type; }
            set { _type = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string fileName
        {
            set { _filename = value; }
            get { return _filename; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string tempName
        {
            set { _tempname = value; }
            get { return _tempname; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TemplateFile(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,fileName,tempName,[type] ");
            strSql.Append(" FROM [TemplateFile] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            DataSet ds = SqliteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    this.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fileName"] != null && ds.Tables[0].Rows[0]["fileName"].ToString() != "")
                {
                    this.fileName = ds.Tables[0].Rows[0]["fileName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["tempName"] != null && ds.Tables[0].Rows[0]["tempName"].ToString() != "")
                {
                    this.tempName = ds.Tables[0].Rows[0]["tempName"].ToString();
                } if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [TemplateFile]");
            strSql.Append(" where id=@id ");

            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            return SqliteHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [TemplateFile] (");
            strSql.Append("fileName,tempName,[Type])");
            strSql.Append(" values (");
            strSql.Append("@fileName,@tempName,@Type)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@fileName", DbType.String),
					new SQLiteParameter("@tempName", DbType.String),
					new SQLiteParameter("@Type", DbType.Int32)};
            parameters[0].Value = fileName;
            parameters[1].Value = tempName;
            parameters[2].Value = Type;

            return SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [TemplateFile] set ");
            strSql.Append("fileName=@fileName,");
            strSql.Append("tempName=@tempName,");
            strSql.Append("[Type]=@Type");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@fileName", DbType.String),
					new SQLiteParameter("@tempName", DbType.String),
					new SQLiteParameter("@id", DbType.Int32,8),
					new SQLiteParameter("@Type", DbType.Int16)};
            parameters[0].Value = fileName;
            parameters[1].Value = tempName;
            parameters[2].Value = id;
            parameters[3].Value = Type;

            int rows = SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [TemplateFile] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            int rows = SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,fileName,tempName,[type] ");
            strSql.Append(" FROM [TemplateFile] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            DataSet ds = SqliteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    this.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fileName"] != null && ds.Tables[0].Rows[0]["fileName"].ToString() != "")
                {
                    this.fileName = ds.Tables[0].Rows[0]["fileName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["tempName"] != null && ds.Tables[0].Rows[0]["tempName"].ToString() != "")
                {
                    this.tempName = ds.Tables[0].Rows[0]["tempName"].ToString();
                } if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    this.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [TemplateFile] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqliteHelper.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

